<?php

/*
 * litchicms
 * Copyright (C) 2010-5-28 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");
check_rights_by_id(25);
$can_action = array('from', 'insert');

if (empty($_POST['action']) || !in_array($_POST['action'], $can_action)) {
    $_POST['action'] = 'from';
}

switch ($_POST['action']) {
    case 'from':

        $rs = brk_select("right");
        while ($row = $Db->fetch_array($rs)) {
            $RIGHT[$row['right_id']] = $row;
        }
        !class_exists('Tree') && require 'tree.class.php';
        $tree = new Tree($RIGHT, 'right_id', 'right_pid', 'right_note');
        unset($RIGHT);
        $RIGHT_TREE = $tree->getChilds();
        array_shift($RIGHT_TREE);
        $RIGHT_TREE = array_flip($RIGHT_TREE);
        foreach ($RIGHT_TREE as $k => &$v) {
            $v = array($k, $tree->getValue($k), $tree->getLayer($k, "|---", 1));
        }

        unset($v);
//        dump($RIGHT_TREE);
        break;
    case 'insert':
         if(preg_match("/[^a-z]/is", $_POST['right_node'])){
            eMsg(0, "填写不正确,节点名只可以是小写英文", 0);
            $_POST['action'] = 'error';
            break;
         }

        if (!brk_select("right", "*", " WHERE right_id='" . intval($_POST['right_pid']) . "'")) {
            $_POST['action'] = 'error';
            eMsg(0, "参数错误");
            break;
        }
        $rs = brk_select("right", "*", " WHERE right_pid='" . intval($_POST['right_pid']) . "'");
        $temp = array();
        while ($row = $Db->fetch_array($rs)) {
            $temp[$row['right_node']] = $row;
        }
        if (key_exists($_POST['right_node'], $temp)) {
            $_POST['action'] = 'error';
            eMsg(0, "你填写的节点名已存在欲添至的父级节点");
            break;
        }
        if(bg_add_right(intval($_POST['right_pid']),$_POST['right_note'],$_POST['right_node'])){
            eMsg(1, "添加成功");
            write_conf_file(65535);
        }
        break;
    default:
        break;
}
?>
